class Solution {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (subRoot == null) {
            return true;
        }
        if (root == null) {
            return false;
        }

        //必定会有子树与所需相等
        return isSubtree(root.left, subRoot)
                || isSubtree(root.right, subRoot)
                || isSameTree(root, subRoot);
    }

    public boolean isSameTree(TreeNode a, TreeNode b) {
        if (a == null && b == null) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }
        if (a.val != b.val) {
            return false;
        }

        return isSameTree(a.left, b.left) && isSameTree(a.right, b.right);
    }
}

// 定义二叉树节点类
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

public class OtherTreeIsSubtree {
    public static void main(String[] args) {
        // 构建示例树
        //       3
        //      / \
        //     4   5
        //    / \
        //   1   2
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(4);
        root.right = new TreeNode(5);
        root.left.left = new TreeNode(1);
        root.left.right = new TreeNode(2);

        // 构建示例子树
        //     4 
        //    / \
        //   1   2
        TreeNode subRoot = new TreeNode(4);
        subRoot.left = new TreeNode(1);
        subRoot.right = new TreeNode(2);

        // 测试 isSubtree 方法
        Solution solution = new Solution();
        boolean result = solution.isSubtree(root, subRoot);
        System.out.println("子树检查结果: " + result);
    }
}    